\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{math article}[2020/06/07 math article class]

\RequirePackage{xkeyval} %kvoptions

\newif\ifprint
\DeclareOptionX{print}{\printtrue}
\newif\ifexam
\DeclareOptionX{exam}{\examtrue}
\newif\ifbook
\DeclareOptionX{article}{\bookfalse}
\DeclareOptionX{book}{\booktrue}
\ifbook
  \DeclareOptionX*{\PassOptionsToClass{\CurrentOption}{book}}
\else
  \DeclareOptionX*{\PassOptionsToClass{\CurrentOption}{article}}
\ExecuteOptionsX{book}
\ProcessOptionsX*\relax
% 为了 \cleardoublepage 起效
\ifprint
  \ifbook
    \PassOptionsToClass{twoside}{book}
  \else 
    \PassOptionsToClass{twoside}{article}
  \fi
\fi
\ifbook
  \LoadClass{book}
\else
  \LoadClass{article}
\fi

\RequirePackage{geometry}
%\geometry{paperheight=260mm,paperwidth=185mm,margin=2cm}% 16K打印
\geometry{b5paper,margin=2cm}
\ifprint
    \geometry{bindingoffset=0.6cm}
\fi

\RequirePackage{microtype}
%\RequirePackage{mparhack} % marginpar 有时给出的位置不对，这是个修正


%%%% 字体设置
\RequirePackage{fontspec,xunicode,xltxtra,xeCJK}
\XeTeXlinebreaklocale "zh"
\XeTeXlinebreakskip = 0pt plus 1pt minus 0.1pt
\setmainfont{NewComputerModern10}
%\setmainfont{XITS}
%\setsansfont{Merriweather Sans Light}[BoldFont={Merriweather Sans},Numbers=Lowercase]
\setsansfont[BoldFont=Myriad Pro]{Myriad Pro Light}
\setmonofont{Noto Sans Mono CJK SC}
\setCJKmainfont{FZShuSong-Z01S}[
    BoldFont=FZYaSong-DB-GBK,
    ItalicFont=STKaiti,
]
\setCJKsansfont[BoldFont=Noto Sans CJK SC]{Noto Sans CJK SC Light}
\setCJKmonofont{STKaiti}
\newcommand\songti{\CJKfontspec{FZShuSong-Z01S}}
\newcommand\heiti{\CJKfontspec{SimHei}}
\newcommand\xinwei{\CJKfontspec{STXinwei}}
\newcommand\fangsong{\CJKfontspec{STFangsong}}
\newcommand\kaiti{\CJKfontspec{STKaiti}}
\newcommand\lishu{\CJKfontspec{STLiti}}
\newcommand\xingkai{\CJKfontspec{STXingkai}}

% 数学设定
\RequirePackage{amsmath,amsthm,unicode-math,mathtools,stmaryrd,multicol,xcolor,extarrows}
\unimathsetup{math-style=TeX}
\setmathfont{NewComputerModernMath}
\setmathfont[range=cal]{NewComputerModernMath}
%\setmathfont{XITS Math}
%\setmathfont{XITS Math}[range=cal]
\setmathfont[range=scr]{XITS Math}

\setlength\multicolsep{\topsep}
\RequirePackage{array}   % for \newcolumntype macro
\newcolumntype{L}{>{$}l<{$}} % math-mode version of "l" column type
\newcolumntype{R}{>{$}r<{$}} 
\newcolumntype{C}{>{$}c<{$}} 

\RequirePackage{ulem}
\RequirePackage{manfnt}
\newenvironment{warning}{\par\noindent{\makebox[0pt][r]{\scriptsize\ifprint\else\color{red!90}\fi\textdbend\quad}\textbf{警告}.\hspace{0.25em}}\kaiti}{\par\vspace{.5\baselineskip}}

% 章节格式
\newlength{\secspacingafter}
\setlength{\secspacingafter}{.5em}
%\RequirePackage[compact]{titlesec}
%\titlespacing*{\section}
%{0pt}{3.5ex plus 1ex minus .2ex}{2.3ex plus .2ex}
\RequirePackage{titlesec}
\ifbook
  \titleformat{\chapter}[hang]{\centering\LARGE\bfseries\sffamily}{第{\zhnumber{\thechapter}}章}{1em}{}
  \titlespacing{\chapter}{0pt}{0em}{3em}
\fi
% 练习中也有设置 section
\titleformat{\section}{\centering\bfseries\sffamily}{\thesection}{\secspacingafter}{}
\titleformat{\subsection}{\bfseries\sffamily}{\thesubsection}{\secspacingafter}{}
\titleformat{\subsubsection}{\bfseries\sffamily}{\thesubsubsection}{\secspacingafter}{}
\setcounter{secnumdepth}{4}
\counterwithout{paragraph}{subsubsection}
\counterwithin*{paragraph}{section}
\titleformat{\paragraph}[runin]{\bfseries\sffamily}{\alph{\theparagraph}}{\secspacingafter}{}
%\RequirePackage{tocloft}
% 居中“目录”名称 https://tex.stackexchange.com/questions/142445/how-do-i-center-the-table-of-contents-title-using-tocloft
%\renewcommand{\cfttoctitlefont}{\hfill\ttfamily}
%\renewcommand{\cftaftertoctitle}{\hfill\hfill}
\ifbook
  \if@twoside
      \def\chaptermark#1{%
        \markboth {\MakeUppercase{%
          \ifnum \c@secnumdepth >\m@ne
            \if@mainmatter
              第 \thechapter 章 \ %
            \fi
          \fi
          #1}}{}}%
  \else
      \def\chaptermark#1{%
        \markright {\MakeUppercase{%
          \ifnum \c@secnumdepth >\m@ne
            \if@mainmatter
              第 \thechapter 章 \ %
            \fi
          \fi
          #1}}}
  \fi
\fi

%%% 放到小节标题后的注记
\def\ignorespacesandallpars{%
  \@ifnextchar\par
    {\expandafter\ignorespacesandallpars\@gobble}%
    {}%
}
\newcommand\hint{\par\noindent {\lishu 提示}：\ignorespacesandallpars}

\numberwithin{equation}{section}
%%%%theorem style
\newtheoremstyle{plainzh}% name
{.5em}% Space above
{.5em}% Space below 
{\kaiti}% Body font
{}% Indent amount
{\bfseries}% Theorem head font
{}% Punctuation after theorem head 
{.5em}% Space after theorem head 
{}% Theorem head spec (can be left empty, meaning ‘normal’ )
\newtheoremstyle{definitionzh}{.5em}{.5em}{}{}{\bfseries}{.}{.5em}{}
\theoremstyle{plainzh}
\ifbook
  \newtheorem{theorem}{定理}[chapter]
\else
  \newtheorem{theorem}{定理}[section]
\fi
\newtheorem*{theorem*}{定理}
\newtheorem{observation}[theorem]{观察}
\newtheorem*{observation*}{观察}
\newtheorem{promise}[theorem]{承诺}
\newtheorem{proposition}[theorem]{命题}
\newtheorem*{proposition*}{命题}
\newtheorem{problem}[theorem]{难题}
\newtheorem{question}[theorem]{问题}
\newtheorem{conjecture}[theorem]{猜想}
\newtheorem{corollary}[theorem]{推论}
\newtheorem*{corollary*}{推论}
\newtheorem{assertion}[theorem]{论断}
\newtheorem{lemma}[theorem]{引理}
\newtheorem*{lemma*}{引理}
\newtheorem*{claim}{断言}
\theoremstyle{definitionzh}
\newtheorem{definition}[theorem]{定义}
\newtheorem*{definition*}{定义}
\newtheorem*{notation}{记号}
\newtheorem{example}[theorem]{例}
\newtheorem*{example*}{例}
%\theoremstyle{remarkzh}
\newtheorem{remark}[theorem]{注记}
\newtheorem*{remark*}{注记}
\newtheoremstyle{exercise}{3pt}{3pt}{}{}{\bfseries}{.}{.5em}%
  {\thmnumber{#2\thmnote{ (#3)}}}
\ifbook
  \theoremstyle{exercise}
  \newtheorem{exercise}{练习}[chapter]
\else
\newtheorem{exercise}[theorem]{练习}
\newtheorem*{exercise*}{练习}
\fi
\renewcommand\proofname{证明}
\newenvironment{solution}{
    \renewcommand\proofname{解答}\begin{proof}
}{\end{proof}\renewcommand\proofname{证明}}
% 给习题打星号: https://tex.stackexchange.com/questions/123392/add-a-marker-to-the-left-of-the-text
\newcommand{\starmark}{\strut\vadjust{\domark}}
\newcommand{\domark}{%
  \vbox to 0pt{
    \kern-\dp\strutbox
    \hbox{\smash{\llap{*\kern.1em}}}
    \vss
  }%
}
% utf 符号
\RequirePackage{utfsym, marginnote,chemformula}



% 用于忽略
\newcommand\aemph[1]{{\fangsong #1}}

%%%% 列表
\RequirePackage[shortlabels]{enumitem}
\setlist{parsep=0em,itemsep=3pt,partopsep=0em,topsep=1ex}
\setlist[enumerate,1]{
    listparindent=\parindent,
    label=\textup{(\arabic*)},
    labelindent=0em,
    itemindent=0em,
    widest,
    align=parleft,
    leftmargin=\parindent,
    labelsep=*,
}
\setlist[enumerate,2]{
    listparindent=\parindent,
    label=\textup{(\roman*)},
    labelindent=0em,
    itemindent=0em,
    widest,
    align=parleft,
    leftmargin=\parindent,
    labelsep=*,
}

%%%% 图片
\RequirePackage{graphicx,tikz,pgfplots,subcaption}
\RequirePackage[export]{adjustbox}
\renewcommand{\figurename}{图}
% 下面的代码使得图片可以占整页，下面保持了长宽比并使得宽度等于页宽
\newcommand\includeFullPageGraphics[2][]{
  \newpage
  \thispagestyle{empty}
  \begin{tikzpicture}[remember picture, overlay, inner sep=0pt]
    \node at (current page.center) 
      {\includegraphics[width=\paperwidth, keepaspectratio=true,#1]{#2}};
  \end{tikzpicture}
  \newpage
}
\usetikzlibrary{
  cd,arrows.meta,calc,matrix,
  petri,positioning,decorations,decorations.text,
  decorations.pathreplacing,pgfplots.fillbetween,
  decorations.markings,intersections,
  calligraphy,shapes.geometric,
}
\tikzset{>=Computer Modern Rightarrow}

\RequirePackage[version=4]{mhchem}

\RequirePackage[stable]{footmisc}
\RequirePackage[
  backend=biber,
  hyperref=true,
  doi=true,
  style=gb7714-2015,
  gbnamefmt=lowercase
]{biblatex}
\renewcommand{\cite}[2][]{\parencite{#2}\ifthenelse{\equal{#1}{}}{}{~#1}}

\RequirePackage[indentafter]{parnotes}
\counterwithin{parnotemark}{section}
%\renewcommand{\theparnotemark}{\thechapter.\thesection.\@arabic\c@parnotemark}
\renewcommand{\parnotecusmarkfmt}[1]{\textsuperscript{\color{red} #1}}
\renewcommand{\parnotefmt}[1]{\normalsize\rmfamily\fangsong\noindent #1}
\renewcommand{\parnotevskip}{\smallskipamount}
\renewcommand\parnoteref[2]{(#1, \ref{#2})}
% 调整定义为了让 mark 变成超链接
\long\def\PN@parnote@real#1#2{%
    \parnotemark{\hyperref[ParnoteTag:#1]{#1}}%
    % Unless this is the first parnote in \PN@text, add a separator first
    \unless\ifx\PN@text\@empty\g@addto@macro\PN@text{\par\noindent}\fi
    % Redefine \@currentlabel to the parnote label, so \label works
    \g@addto@macro\PN@text{\phantomsection\def\@currentlabel{#1}\label{ParnoteTag:#1}}%
    \g@addto@macro\PN@text{\parnotemark{#1}\nolinebreak\thinspace#2}%
}
%\g@addto@macro\@endtheorem{\parnotes}
% 这个定义是多余的，autopn 环境中自动地定理等中的注记都在最后，定理等整体地被当作一段
%\def\theoremlist{
%  abstract, assertion, caution, claim, complement, conjecture, 
%  construction, corollary, definition, example, examples, 
%  exercise, lemma, notation, note, notes, observation, principle,
%  problem, proposition, question, remark, remarks, theorem, 
%}
%\@for\next:=\theoremlist\do{
%  \expandafter\g@addto@macro\csname end \next\endcsname{\noexpand\parnotes}
%  \expandafter\g@addto@macro\csname end \next star\endcsname{\noexpand\parnotes}
%}
%\g@addto@macro\endproof{\parnotes}



% 目录与章节的排版
\renewcommand{\contentsname}{目录}
\renewcommand\appendixname{附录}
\setcounter{tocdepth}{1}

\RequirePackage{indentfirst}
\setlength{\parindent}{2em}
\RequirePackage{setspace}

\let\oldtitle\title
\ifbook
  \def\title#1{\oldtitle{\begin{Huge}{\bfseries\sffamily #1}\end{Huge}}}
\else
  \def\title#1{\oldtitle{\begin{large}{\bfseries\sffamily #1}\end{large}}}
\fi

\RequirePackage{zhnumber}

\usepackage{imakeidx} 
\makeindex[title={名词索引}]

\RequirePackage{hyperref}
\hypersetup{	
    bookmarks=true,
    unicode=true,
    psdextra,
    colorlinks=true,
    pdfborder={0,0,0},
}

\ifprint
  \hypersetup{
    hidelinks,
    colorlinks=false,
  }
\fi

%%% 文档信息和个人信息
\RequirePackage{datetime}
\newdateformat{mydate}{\THEYEAR 年 \THEMONTH 月 \THEDAY 日}
\date{\mydate\today}
\author{}%sc@hsu.edu.cn}
\newcommand\printpersonalinfo{\par\vspace{20pt}\textbf{邮箱}：sc@hsu.edu.cn\hspace{1em}\textbf{地址}：中国安徽省黄山市，黄山学院数统学院\hspace{1em}\textbf{个人主页}：\url{https://gitee.com/maizisc}}

% 自定义有用的命令
% 圆圈数字用于标记步骤之类
\newcommand\circled[1]{\raisebox{1pt}{\tikz[baseline=(char.base)]{\node[circle,draw,inner sep=1pt] (char) {\scriptsize #1};}}}

\input{symbols.sty}
